package com.example.myapplication.Media;

import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.R;

public class VideoActivity extends AppCompatActivity {
  VideoView videoView;
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.media_video_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("视频播放");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    SetVideoView();
    SetButton();
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }
  private void GoBack() {
    finish();
  }

  private void SetVideoView() {
    videoView = findViewById(R.id.videoView);
    String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.test_video; // 视频文件的路径
    videoView.setVideoURI(Uri.parse(videoPath));//设置视频源
    //videoView.start();
    // 请求获取焦点，用于响应用户的控制操作
    videoView.requestFocus();
  }
  private void SetButton() {
    Button playButton = (Button) findViewById(R.id.play_button);
    Button pauseButton = (Button) findViewById(R.id.pause_button);
    playButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        videoView.start();
      }
    });
    pauseButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        videoView.pause();
      }
    });
  }
}
